<?php

/**
 * This is the model class for table "ykl_video".
 *
 * The followings are the available columns in table 'ykl_video':
 * @property integer $id
 * @property string $title
 * @property string $url
 * @property string $content
 * @property string $cover
 * @property string $filesize
 * @property integer $hit
 * @property integer $downloads
 * @property integer $orderby
 * @property integer $categoryid 
 * @property string $createtime
 */
class Video extends CActiveRecord
{
	 var $category_name='';
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ykl_video';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, url, content', 'required','message'=>'不能为空'),
			array('hit, downloads, orderby, categoryid', 'numerical', 'integerOnly'=>true),
			array('title, url, filesize', 'length', 'max'=>128),
            array('cover',
                'file', //定义为file类型
                'allowEmpty' => true,
                'types' => 'jpg,png,gif', //上传文件的类型
                'maxSize' => 1024 * 1024 * 10, //上传大小限制，注意不是php.ini中的上传文件大小
                'tooLarge' => '文件大于10M，上传失败！请上传小于10M的文件！'
            ),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, title, url, content, cover, filesize, hit, downloads, orderby, categoryid, createtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			 'category'=>array(self::BELONGS_TO,'Category','categoryid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => '标题',
			'url' => '文件地址',
			'content' => '简介',
			'cover' => '封面',
			'filesize' => '文件大小(MB)',
			'hit' => '点击量',
			'downloads' => '下载量',
			'orderby' => '是否置顶',
			'categoryid' => '所属分类', 
			'category_title' =>'分类名称', 
			'createtime' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('cover',$this->cover,true);
		$criteria->compare('filesize',$this->filesize,true);
		$criteria->compare('hit',$this->hit);
		$criteria->compare('downloads',$this->downloads);
		$criteria->compare('orderby',$this->orderby);
		$criteria->compare('categoryid',$this->categoryid); 
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('category.name',$this->category_name,true);
        $criteria->order="t.id desc";

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Video the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
